<?php

namespace app\staff\controller;

use app\staff\model\StaffNavModel;
use app\staff\model\StaffTypeModel;
use think\admin\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class StaffType extends Controller
{

    /**
     * 当前操作数据库
     * @var string
     */
    private string $table = 'StaffType';

    /**
     * 员工权限管理
     * @auth true
     * @menu true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $this->title = '员工权限管理';

        $this->_query(StaffTypeModel::class)->like('name')->equal('type')->order('id', 'DESC')->page(true, true);
    }

    /**
     * 新增
     * @auth true
     * @return void
     */
    public function add()
    {
        StaffTypeModel::mForm('form');
    }

    /**
     * 修改
     * @auth true
     * @return void
     */
    public function edit()
    {
        StaffTypeModel::mForm('form');
    }

    /**
     * 删除
     * @auth true
     * @throws DbException
     */
    public function remove()
    {
        $this->_delete(StaffTypeModel::class);
    }


    /**
     * 权限
     * @auth true
     * @return void
     */
    public function auth()
    {
        StaffTypeModel::mForm();
    }

    /**
     * @param array $data
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function _form_filter(array &$data)
    {
        if ($this->request->action() == 'auth') {
            if ($this->request->isGet()) {
                $data['rule'] = str2arr($data['rule'] ?? '');
                $this->rule = StaffNavModel::items(['is_deleted' => 0]);
            } elseif ($this->request->isPost()) {
                $data['rule'] = arr2str($data['rule'] ?? []);
            }
        }

    }
}